using System;
using DBComponent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace project
{
    public partial class Close_Stock : Form
    {
        string query;
        OleDbConnection conn;
        OleDbCommand command;
        int closing_stock;
        string pname;
        string mname;
        DateTime mdate;
        int price;
        AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();

        public Close_Stock()
        {
            InitializeComponent();
            DBConnection();
        }

        private void DBConnection()
        {
            try
            {
                conn = DBComponent.Mag_DealerDB.dbconnection();
                conn.Open();
                command = new OleDbCommand();
                command.Connection = conn;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (ValidateCloseStock())
            {
                mname = textBox1.Text;
                PopulateComboBox();
            }

        }

        private bool ValidateCloseStock()
        {
            //TODO
            return true;
        }

        private void PopulateComboBox()
        {
            query = "select MDATE from magazine_selling where MNAME='" + textBox1.Text + "';";
            OleDbCommand command = new OleDbCommand(query, conn);
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                comboBox1.Items.Add(reader[0].ToString());
            }
            reader.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (ValidateCloseStock())
            {
                mdate = DateTime.Parse(comboBox1.SelectedItem.ToString());
                closing_stock = GetClosingStock();
                pname = GetPname();
                UpdateMagazine_ClosedStock();
                //UpdateTransactions();
                UpdateMagazine_Selling();
            }
        }

        private void UpdateMagazine_Selling()
        {
            query = "delete from magazine_selling where MNAME='" + mname + "' and MDATE=#" + mdate + "#;";
            command.CommandText = query;
            command.ExecuteNonQuery();

        }

        private void UpdateMagazine_ClosedStock()
        {
            int unsold = closing_stock;

            query = "insert into magazine_closedstock values('" + mname + "',#" + mdate + "#,'" + pname + "','" + unsold + "');";
            /* OleDbParameter param = new OleDbParameter("mname", mname);
             param.OleDbType = OleDbType.VarChar;
             command.Parameters.Add(param);
             command.Parameters.Clear();
             param.ParameterName = "mdate";
             param.Value = mdate;
             param.OleDbType = OleDbType.Date;
             command.Parameters.Add(param);
             command.Parameters.Clear();
             param.ParameterName = "pname";
                 param.Value = pname;
             param.OleDbType = OleDbType.VarChar;
             command.Parameters.Add(param);
             command.Parameters.Clear();
             param.ParameterName = "unsold";
             param.Value = unsold;
             param.OleDbType = OleDbType.Integer;*/
            //command.Parameters.Add(param);
            /*            command.Parameters.Add(mname, OleDbType.VarChar).value = mname;
                        command.Parameters.Add(mdate, OleDbType.Date).value = mdate;
                        command.Parameters.Add(pname, OleDbType.VarChar).value = pname;
                        command.Parameters.Add(unsold, OleDbType.Integer).Value = unsold;
                        command.CommandText = query;*/
            command.CommandText = query;
            command.ExecuteNonQuery();

        }

        private string GetPname()
        {
            query = "select PNAME from magazine where MNAME='" + mname + "';";
            command.CommandText = query;
            OleDbDataReader reader = command.ExecuteReader();
            string pname = "";
            while (reader.Read())
            {
                pname = reader[0].ToString();
            }
            reader.Close();
            return pname;

        }

        private int GetClosingStock()
        {
            int closingStock = 0;

            query = "select CLOSING_STOCK from stock_date where MNAME='" + mname + "' and MDATE=#" + mdate + "#;";
            command.CommandText = query;
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                closingStock = int.Parse(reader[0].ToString());
            }
            reader.Close();

            return closingStock;

        }

        private void Close_Stock_Load(object sender, EventArgs e)
        {
            try
            {
                DBConnection();
                OleDbDataReader reader;
                command.CommandText = "select MNAME from magazine;";
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    namesCollection.Add(reader[0].ToString());
                }

                reader.Close();
                textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
                textBox1.AutoCompleteCustomSource = namesCollection;
            }
            catch (Exception ex)
            {
            }
        }


    }
}